﻿@{
    ViewBag.Title = "回访操作页面";
    Layout = "~/Areas/IM/Views/Shared/_Layout.cshtml";
}

@section Scripts{
    <script type="text/javascript">

        $(document).ready(function () {
            RevistAddMainGrid();
        });

        $(window).resize(function () {
            $.setGridWithSearchBar("#RevisitOperate", "tbb", "#RevisitOperateDetailList");
        });

        var LastIndex;//全局变量
        var selectState = 2;//全局变量,默认2， 进来时是未回访的内容。


        function RevistAddMainGrid() {
            $('#RevisitOperateDetailList').datagrid({
                width: 'auto',
                height: 'auto',
                //fitColumns: true, //真正的自动展开/收缩列的大小，以适应网格的宽度，防止水平滚动。
                loadMsg: '数据加载中,请稍后……',
                url: '@Url.Content("~/DD/Revisit/GetRevistAddDetail")',
                striped: true,
                singleSelect: true,
                //  pagination: false,//不启用分页，默认每页10行
                rownumbers: true,//显示页码，默认 提供 10 - 50 的页容量选择下拉框
                //pageSize: 15,//设置 页容量5
                //pageList: [10, 15, 20],//设置 页容量下拉框
                queryParams: {
                    ID: '@ViewData["RowID"]',
                    ifOperate: selectState

                },
                frozenColumns: [[
                     { field: "ID", hidden: true },
                { field: "taskCode", hidden: true },
                 {
                     field: "RevisitState", hidden: true,
                     editor: {
                         type: 'text'
                         //    options:
                         //{
                         //}
                     }
                 },
                {
                    field: "RevisitWay", title: "回访方式", width: '5%', align: "center",
                    editor: {
                        type: 'combobox',
                        options:
                        {
                            valueField: 'Name',
                            textField: 'Name',
                            panelHeight: '60',
                            // method: 'post',
                            editable: false,
                            url: '@Url.Content("~/IM/AllDictionary/GetMSDictionaryInfo?TypeCode=RevisitType")'
                        }
                    }
                },
                {
                    field: "Satisfaction", title: "满意度", width: '9%', align: "center",
                    editor: {
                        type: 'combobox',
                        options:
                        {
                            valueField: 'Name',
                            textField: 'Name',
                            panelHeight: '110',
                            // method: 'post',
                            editable: true,
                            url: '@Url.Content("~/IM/AllDictionary/GetMSDictionaryInfo?TypeCode=SatisfactionType")'
                        }
                    }
                },

                {
                    field: "NotSatisfactionReson", title: "不满意原因", width: '12%', align: "center",
                    editor: {
                        type: 'combobox',
                        options:
                                {
                                    method: 'post',
                                    valueField: 'Name',
                                    textField: 'Name',
                                    panelHeight: '240',
                                    editable: true,
                                    url: '@Url.Content("~/IM/AllDictionary/GetMSDictionaryInfo?TypeCode=NotSatisfaction")'
                                }
                    }
                }
                ]],

                columns: [[
                { field: "Station", title: "分站", width: '5%', align: "center" },
                { field: "Ambluance", title: "车辆", width: '6%', align: "center" },
                { field: "AlarmTel", title: "呼救电话", width: '7%', align: "center" },
                { field: "LinkCall", title: "联系电话", width: '7%', align: "center" },
                { field: "LinkMan", title: "联系人", width: '5%', align: "center" },
                { field: "LocalAddr", title: "现场地址", width: '12%', align: "center" },
                { field: "PatientName", title: "患者姓名", width: '5%', align: "center" },
                { field: "Sex", title: "性别", width: '3%', align: "center" },
                { field: "Age", title: "年龄", width: '3%', align: "center" },
                { field: "AlarmReason", title: "主诉", width: '7%', align: "center" },
                { field: "SendAddr", title: "送往地址", width: '10%', align: "center" },
                { field: "StartTaskTime", title: "生成任务时刻", width: '10%', align: "center", formatter: $.formatTime },
                { field: "DispatcherName", title: "责任调度员", width: '5%', align: "center" },
                { field: "Driver", title: "司机", width: '5%', align: "center" },
                { field: "Doctor", title: "医生", width: '5%', align: "center" },
                { field: "Stretcher", title: "担架员", width: '5%', align: "center" }
                ]],
                toolbar: [
                 {
                     // text: '提交回访！',
                     text: '<span style="color:red;font-size:large">提交回访结果</span>',
                     iconCls: 'icon-custom-cGoods',
                     iconWidth: 22,
                     handler: function () {


                         var edssq = $('#RevisitOperateDetailList').datagrid('getEditor', { index: LastIndex, field: 'Satisfaction' });
                         var productnamezq = $(edssq.target).combobox('getText');
                         if (productnamezq.length > 0) {
                             var edqqq = $('#RevisitOperateDetailList').datagrid('getEditor', { index: LastIndex, field: 'RevisitState' }); //这里解决了问题1
                             var ressqq = $(edqqq.target).val("已回访");
                             $('#RevisitOperateDetailList').datagrid('getRows')[LastIndex]['RevisitState'] = ressqq;
                         }

                         $('#RevisitOperateDetailList').datagrid('endEdit', LastIndex); //结束编辑最后一行
                         editIndex = undefined;
                         submitRevisitDetail();
                     }
                 }
                ],
                onClickRow: onClickRow,

                onLoadSuccess: function (data) {

                    $.messager.show({
                        title: '系统提示',
                        height: '130px',
                        width: '320px',
                        msg: '   成功加载出 ' + data.total + ' 条任务信息！'
                    });


                    // changeDeliveryViewNum();
                }
                //onEndEdit: function (index, row, changes)
                //{

                //}

            });
            $.setGridWithSearchBar("#RevisitOperate", "tbb", "#RevisitOperateDetailList");
            setToolTips();

        }

        //参考easyUI官网，点击单行出现combobox，供勾选值。  问题1： 有一个小问题，当选最后一行时，点击提交回访，无法结束编辑。
        var editIndex = undefined;
        function endEditing() {
            if (editIndex == undefined) {
                return true;
            }
           
            if ($('#RevisitOperateDetailList').datagrid('validateRow', editIndex) || selectState == 2)//验证指定的行，当验证有效的时候返回true。  editIndex 实际为Index
            {
                var edd = $('#RevisitOperateDetailList').datagrid('getEditor', { index: editIndex, field: 'RevisitWay' });
                var productnamef = $(edd.target).combobox('getText');
                $('#RevisitOperateDetailList').datagrid('getRows')[editIndex]['RevisitWay'] = productnamef;

                var ed = $('#RevisitOperateDetailList').datagrid('getEditor', { index: editIndex, field: 'NotSatisfactionReson' });
                var productname = $(ed.target).combobox('getText');
                $('#RevisitOperateDetailList').datagrid('getRows')[editIndex]['NotSatisfactionReson'] = productname;

                var edss = $('#RevisitOperateDetailList').datagrid('getEditor', { index: editIndex, field: 'Satisfaction' });
                var productnamez = $(edss.target).combobox('getText');
                $('#RevisitOperateDetailList').datagrid('getRows')[editIndex]['Satisfaction'] = productnamez;

                //判断当有满意度内容的时候将该条记录更改状态为“已回访”
                if (productnamez.length > 0) {
                    var edq = $('#RevisitOperateDetailList').datagrid('getEditor', { index: editIndex, field: 'RevisitState' });
                    var ress = $(edq.target).val("已回访");
                    $('#RevisitOperateDetailList').datagrid('getRows')[editIndex]['RevisitState'] = ress;
                }
                $('#RevisitOperateDetailList').datagrid('endEdit', editIndex); //结束编辑
                //   $('#RevisitOperateDetailList').datagrid('highlightRow', editIndex); //高亮已经编辑完成的行
                LastIndex = editIndex; //最后编辑行  给全局变量
                editIndex = undefined;

                return true;
            }
            else { return false; }
        }
        function onClickRow(index) {

            if (editIndex != index) {

                if (endEditing()) {
                    $('#RevisitOperateDetailList').datagrid('selectRow', index)
                                                  .datagrid('beginEdit', index);
                    editIndex = index;
                    LastIndex = index;
                }
                else {

                    $('#RevisitOperateDetailList').datagrid('selectRow', editIndex);
                }
            }

        }
        var get = new Array();
        function GetRevisitSelecteds() {
            var getAllRow = $('#RevisitOperateDetailList').datagrid('getRows');
            $.each(getAllRow, function (i, infos) {
                if (infos.RevisitState == "已回访") {
                    get.push(getAllRow[i]);
                }
            });

            var postArray = new Array();
            for (var i = 0; i < get.length; i++) {
                if (get[i] != null) {

                    var obj = new Object();
                    obj.taskCode = get[i].taskCode;
                    obj.RevisitState = get[i].RevisitState;
                    obj.Satisfaction = get[i].Satisfaction;
                    obj.RevisitWay = get[i].RevisitWay;
                    obj.NotSatisfactionReson = get[i].NotSatisfactionReson;
                    obj.RevistPerson = '@ViewData["RevistPerson"]';
                    obj.RevistTime = $.GetToday();
                    obj.ID = get[i].ID;

                    postArray.push(obj);
                }
            }
            return postArray;
        }

        function submitRevisitDetail() {
            var isValid = $("#RevisitOperateDetailList").form("validate");
            if (isValid) {
                var revisitrows = new Object();
                revisitrows = GetRevisitSelecteds();
                if (revisitrows.length > 0) {

                    var str = JSON.stringify(revisitrows);//将JSON转为字符串
                    $.ajax({
                        url: '@Url.Content("~/DD/Revisit/SaveRevistAddDetail")',
                        type: "POST",
                        data: {
                            RevisitDetail: str
                        },
                        success: function (jsData) {
                            $.handleJsonResult(jsData, function () {
                                $('#RevisitOperateDetailList').datagrid('reload');
                                parent.window.opener.RevistMainGrid(); //调用父页面查询方法--刷新
                            });
                        }
                    });
                }
                else {
                    $.messager.alert('系统提示', '当前页面未填写回访结果，请您勾选回访结果后再提交！');
                }
            }

        }

        function SelectApgarScore()  //勾选radio 时实现的方法。
        {
            selectState = GetCheckBox("rblMeiFenZhongXinLv");
            RevistAddMainGrid();
        }
        function GetCheckBox(Name) {
            var chk_value;
            $('input[name="' + Name + '"]:checked').each(function () {
                chk_value = ($(this).val());
            });
            return chk_value;
        }


        function setToolTips() {

            var pTimesDetial = '友情提示：当您有其他办公事宜需要暂时离开，当页未全部完成回访操作，可先将回访结果提交，避免误操作。';
            $('#ptooltipTT').tooltip({
                position: "bottom",
                trackMouse: true,
                content: pTimesDetial,
                onShow: function () {
                    $(this).tooltip('tip').css({
                        backgroundColor: '#DCDAEF',
                        borderColor: '#DCDAEF'
                    });
                }
            });
        }





    </script>
}
<div id="RevisitOperate" class="easyui-layout" style="width:auto; height: auto; margin-left: auto; margin-right: auto">
    <div data-options="region:'center'" style="background: #fafafa; overflow: hidden">
        <div id="tbb" data-options="region:'north'" style="overflow: hidden; padding: 10px">
            <table style="width:99%" cellpadding="0" cellspacing="0" border="0">
                <tr>

                    <td style="text-align:center">
                        可点击筛选→_→
                        <span id="rblMeiFenZhongXinLv" onchange="SelectApgarScore()">
                            <input id="rblMeiFenZhongXinLv_0" type="radio" name="rblMeiFenZhongXinLv" value="2" /><label for="rblMeiFenZhongXinLv_0">未回访</label>
                            <input id="rblMeiFenZhongXinLv_1" type="radio" name="rblMeiFenZhongXinLv" value="1" /><label for="rblMeiFenZhongXinLv_1">已回访</label>
                            <input id="rblMeiFenZhongXinLv_2" type="radio" name="rblMeiFenZhongXinLv" value="-1" /><label for="rblMeiFenZhongXinLv_2">全部</label>
                        </span>
                    </td>

                    <td><label id="ptooltipTT" style="color:blue">这里是提示</label></td>
                </tr>
            </table>
        </div>
        <div data-options="region:'center'" style="background: #fafafa; overflow: hidden">
            <table id="RevisitOperateDetailList" border="0"></table>
        </div>
    </div>
</div>


